package com.hjwei.reflect;

import java.lang.reflect.Method;

//-XX:+PrintGC -Djava.lang.Integer.IntegerCache.high=128
public class Reflect {


    public static void target(int i){
//        new Exception("#"+i).printStackTrace();
    }

    public static void main(String[] args) throws Exception {

        //会调用本地方法
        Class<?> aClass = Class.forName("com.supreme.jvm.Reflect");
        //Class.getMethod 则会遍历该类的公有方法。如果没有匹配到，它还将遍历父类的公有方法
        Method target = aClass.getMethod("target", int.class);
        target.setAccessible(true);
//        polluteProfile();

        long l = System.currentTimeMillis();
        for (int i = 0; i < 2000000000; i++) {
            if(i%100000000==0){
                long l1 = System.currentTimeMillis();
                System.out.println(l1-l);
                l = l1;
            }
            target.invoke(null,128);
        }

    }

    public static void polluteProfile() throws Exception {
        Method method1 = Reflect.class.getMethod("target", int.class);
        Method method2 = Reflect.class.getMethod("target", int.class);
        for (int i = 0; i < 2000; i++) {
            method1.invoke(null, 0);
            method2.invoke(null, 0); }
    }
}
